<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends FrontendController
{
	function __construct()
	{
		parent::__construct();
		$this->lang->load('post');
		$this->load->model('post_model');
		$this->load->model('user_model');
		$this->load->model('category_model');
	}
	public function index($category_id=0, $page=0)
	{
		$page = intval(str_replace('page-', '', $page));
		$category_id = intval($category_id);
		$limit = $this->config->item('per_page');
		if($page < 1) $page = 1;
		$limit_start = ($page - 1) * $limit;

		$category = $this->category_model->get($category_id);
		if(!$category)
		{
			return;
		}
		$category_ids = array($category->id);
		$category_ids = array_merge($category_ids, $category->child);
		$where = 'category_id IN ( '.implode(',', $category_ids).' )';

		$topnews = 1;
		$this->post_model->list_where = 'published = 1 AND type != 2 AND '.$where;
		$items = $this->post_model->getList($limit+$topnews, $limit_start);

		$total = $this->post_model->getTotal();

		$this->post_model->list_where = 'published = 1 AND type = 2 AND '.$where;
		$this->post_model->list_orderby = array('hot desc', 'id desc');
		$videos = $this->post_model->getList(8);

		$this->load->library('pagination');
		$link = siteUrl('post/index/'.$category->id);
		$this->pagination->initialize( array(
			'base_url' => $link,
			'first_url' => $link,
			'total_rows' => $total,
			'per_page' => $limit,
			'prefix' => 'page-',
			'cur_page' => $page,
		) );
		$breadcrumbs = array();
		if($category->parent_id)
		{
			$parent = $this->category_model->get($category->parent_id);
			$breadcrumbs[] = array('href'=>siteUrl('post/index/'.$parent->id), 'title'=>$parent->title);
		}
		$breadcrumbs[] = array('href'=>siteUrl('post/index/'.$category->id), 'title'=>$category->title);
		$data = array(
			'link'=>$link,
			'category'=>$category,
			'items'=>$items,
			'videos'=>$videos,
			'breadcrumbs'=>$breadcrumbs,
		);

		$this->output->set_common_meta($category->title, $category->description, $category->tag);
		$this->load->view('frontend/post/category', $data);
	}

	public function tag($tag='', $page=0)
	{
		$page = intval(str_replace('page-', '', $page));
		$limit = $this->config->item('per_page');
		if($page < 1) $page = 1;
		$limit_start = ($page - 1) * $limit;

		$tag = urldecode($tag);
		if(!$tag)
		{
			return;
		}

		$where_like = $this->db->escape('%'.$this->db->escape_like_str($tag).'%');
		$where = '( tag LIKE '.$where_like.' )';

		$topnews = 1;
		$this->post_model->list_where = 'published = 1 AND type != 2 AND '.$where;
		$items = $this->post_model->getList($limit+$topnews, $limit_start);

		$total = $this->post_model->getTotal();

		$this->post_model->list_where = 'published = 1 AND type = 2 AND '.$where;
		$this->post_model->list_orderby = array('hot desc', 'id desc');
		$videos = $this->post_model->getList(8);

		$this->load->library('pagination');
		$link = siteUrl('post/tag/'.$tag);
		$this->pagination->initialize( array(
			'base_url' => $link,
			'first_url' => $link,
			'total_rows' => $total,
			'per_page' => $limit,
			'prefix' => 'page-',
			'cur_page' => $page,
		) );
		$breadcrumbs = array();
		$breadcrumbs[] = array('href'=>'javascript:;', 'title'=>lang('tag'));
		$breadcrumbs[] = array('href'=>siteUrl('post/tag/'.$tag), 'title'=>$tag);
		$data = array(
			'link'=>$link,
			'title'=>$tag,
			'items'=>$items,
			'videos'=>$videos,
			'breadcrumbs'=>$breadcrumbs,
		);

		$this->output->set_title($tag);
		$this->load->view('frontend/post/search', $data);
	}

	public function search($page=0)
	{
		$page = intval(str_replace('page-', '', $page));
		$limit = $this->config->item('per_page');
		if($page < 1) $page = 1;
		$limit_start = ($page - 1) * $limit;

		$keyword = $this->input->get('keyword');
		if(!$keyword)
		{
			return;
		}

		$where_like = $this->db->escape('%'.$this->db->escape_like_str($keyword).'%');
		$where = '( tag LIKE '.$where_like.' OR title LIKE '.$where_like.' )';

		$topnews = 1;
		$this->post_model->list_where = 'published = 1 AND type != 2 AND '.$where;
		$items = $this->post_model->getList($limit+$topnews, $limit_start);

		$total = $this->post_model->getTotal();

		$this->post_model->list_where = 'published = 1 AND type = 2 AND '.$where;
		$this->post_model->list_orderby = array('hot desc', 'id desc');
		$videos = $this->post_model->getList(8);

		$this->load->library('pagination');
		$link = siteUrl('post/search/?keyword='.$keyword);
		$this->pagination->initialize( array(
			'base_url' => $link,
			'first_url' => $link,
			'total_rows' => $total,
			'per_page' => $limit,
			'prefix' => 'page-',
			'cur_page' => $page,
		) );
		$breadcrumbs = array();
		$breadcrumbs[] = array('href'=>'javascript:;', 'title'=>lang('search'));
		$breadcrumbs[] = array('href'=>$link, 'title'=>$keyword);
		$data = array(
			'link'=>$link,
			'title'=>$keyword,
			'items'=>$items,
			'videos'=>$videos,
			'breadcrumbs'=>$breadcrumbs,
		);

		$this->output->set_title($keyword);
		$this->load->view('frontend/post/search', $data);
	}

	public function detail($id=0)
	{
		$id = intval($id);
		$post = $this->post_model->getItem($id);
		if(!$post)
		{
			return;
		}
		$post->content = preg_replace("/\<img([^\>\r\n]*?)src=[\'\"]([^\'\"\>\r\n]+?)[\'\"]([^\>]*?)\>/", '<img$1src="'.base_url().trim($this->config->item('folder_image'), '/').'/$2"$3>', $post->content);
		$related = array();
		$tags = preg_split('/\s*,\s*/',trim($post->tag),-1,PREG_SPLIT_NO_EMPTY);
		if(!empty($tags))
		{
			$where_tag = array();
			foreach($tags as $tag)
			{
				$tag = $this->db->escape('%'.$this->db->escape_like_str($tag).'%');
				$where_tag[] = 'tag LIKE '.$tag;
			}
			$this->post_model->list_where = 'published = 1 AND type != 2 AND id <> '.$id.' AND ('.implode(' OR ', $where_tag).')';
			$related = $this->post_model->getList(5);
		}

		$ids = array($id);
		foreach($related as $item)
		{
			$ids[] = $item->id;
		}
		$creator = $this->user_model->getItem($post->created_by);
		$category = $this->category_model->get($post->category_id);

		$this->post_model->list_where = 'published = 1 AND type != 2 AND id NOT IN ('.implode(', ',$ids).') AND category_id = '.$post->category_id;
		$category->posts = $this->post_model->getList(11);

		$breadcrumbs = array();
		if($category->parent_id)
		{
			$parent = $this->category_model->get($category->parent_id);
			$breadcrumbs[] = array('href'=>siteUrl('post/index/'.$parent->id), 'title'=>$parent->title);
		}
		$breadcrumbs[] = array('href'=>siteUrl('post/index/'.$category->id), 'title'=>$category->title);
		//$breadcrumbs[] = array('href'=>siteUrl('post/detail/'.$post->id), 'title'=>$post->title);
		$data = array(
			'breadcrumbs'=>$breadcrumbs,
			'post'=>$post,
			'creator'=>$creator,
			'category'=>$category,
			'related'=>$related,
		);

		$this->output->set_meta('type', 'article');
		$this->output->set_meta('url', siteUrl('post/detail/'.$post->id.'-'.$post->alias, $post->category_id));
		$this->output->set_meta('image', trim($this->config->item('folder_image'), '/').'/'.$post->image);

		$this->output->set_common_meta($post->title, $post->description, $post->tag);

		$this->load->view('frontend/post/detail', $data);
	}
}
